package com.mbc.hr.manager;

import java.sql.SQLException;
import java.util.List;
import com.mbc.common.data.DataList;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.ForeignConflictException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.hr.entities.Institute;

public class InstituteMgr extends AbstractMgr{
	
	public InstituteMgr(){
		
	}
	
	public InstituteMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public void createInstitute(Institute institute)throws Exception{
		openConnection();
		try {
			beginTransaction();
			long l_key  = KeyGenerator.generateKey(entityManager);
			
			//long l_key = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
			
			institute.setInstituteID(l_key);
			entityManager.persist(institute);
			commitTransaction();
		}
		catch (Exception e) {
			rollbackTransaction();
			if(DataDuplicationException.isUniqueKyeError(e))
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally{
			closeConnection();
		}
	}
	
	public void updateInstitute(Institute institute)throws Exception{
		openConnection();
		try {
			beginTransaction();
			
			entityManager.merge(institute);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void deleteInstitute(long instituteID)throws Exception{
		openConnection();
		try {
			beginTransaction();
			Institute l_institute = entityManager.find(Institute.class, instituteID);
			entityManager.remove(l_institute);
			commitTransaction();
		} catch (Exception e) {
			rollbackTransaction();
			if(e.getCause().getCause().getCause() instanceof SQLException){
				if(ForeignConflictException.FOREIGN_CONFLICT_ERROR_CODE == ((SQLException)e.getCause().getCause().getCause()).getErrorCode())
					throw new ForeignConflictException(e);
				else
					throw e;
			}
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	
	public Institute findInstitute(long instituteID)throws Exception{
		openConnection();
		try {
			Institute l_institute = entityManager.find(Institute.class, instituteID);
			return l_institute;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<Institute> findAllInstitute()throws Exception{
		openConnection();
		try {
			return entityManager.createNamedQuery("Institute.findAll", Institute.class).getResultList();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	
	
	public DataList<Institute> findInstitutebyCriteria(CriteriaParams cri) throws Exception{
		openConnection();
		try {
            String l_jpql = "select lt from Institute lt where lt.instituteName like '%" + cri.getKeyword() + "%'";
            
            List<Institute> l_entityList = entityManager.createQuery(l_jpql, Institute.class)
                    .setFirstResult(cri.getIndex())
                    .setMaxResults(cri.getSize())
                    .getResultList();
            
            DataList<Institute> l_dataList = new DataList<Institute>();//LeaveSetupDataList();
            l_dataList.setEntityList(l_entityList);
            
            if(cri.getIndex() == 0){
            	String l_countQuery = "select count(lt.instituteName) from Institute lt where lt.instituteName like '%" + cri.getKeyword() + "%'";
            	l_dataList.setTotalRecord(Long.parseLong(entityManager.createQuery(l_countQuery).getSingleResult().toString()));
            }
			return l_dataList;
		} catch (Exception e) {
			throw e;
		} finally{
			closeConnection();
		}	
	}

}
